/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "BlenderTextureLoader.h"

#include "OgreTexture.h"

#include "Logger.h"

namespace GNgine
{
	BlenderTextureLoader::BlenderTextureLoader(Blender::Image *bImage)
		: _bImage(bImage)
	{
		assert(_bImage);
	}

	BlenderTextureLoader::~BlenderTextureLoader()
	{
	}

	void BlenderTextureLoader::loadResource(Ogre::Resource* resource)
	{
		assert(_bImage);
		assert(resource);

		Ogre::Texture* texture=static_cast<Ogre::Texture*>(resource);
		/// internal packed file data
		if(_bImage->packedfile==0||_bImage->packedfile->data==0){
			GNgLog.out("Warning: Skipping image %s no packed file information is present!", texture->getName().c_str());
			return;
		}
		Blender::PackedFile *pack=_bImage->packedfile;
		unsigned char* raw=((unsigned char*)pack->data);
		int size=pack->size;

		Ogre::DataStreamPtr stream=Ogre::DataStreamPtr(new Ogre::MemoryDataStream(raw,size));
		Ogre::Image image;
		image.load(stream);

		texture->setUsage(Ogre::TU_DEFAULT);
		texture->setTextureType(Ogre::TEX_TYPE_2D);
		texture->setNumMipmaps(5);
		texture->setWidth(image.getWidth());
		texture->setHeight(image.getHeight());
		texture->setDepth(image.getDepth());
		texture->setFormat(image.getFormat());

		Ogre::ConstImagePtrList list;
		list.push_back(&image);
		texture->_loadImages(list);
	}

}
